package com.ruoyi.develop.goodsCustomer.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysArea;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.develop.goodsOrderIn.service.IGoodsOrderInService;
import com.ruoyi.develop.goodsSale.domain.GoodsSale;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.develop.goodsCustomer.domain.GoodsCustomer;
import com.ruoyi.develop.goodsCustomer.service.IGoodsCustomerService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 客户信息管理Controller
 * @author qh
 * @date 2023-01-05
 */
@RestController
@RequestMapping("/goodsCustomer/goodsCustomer")
public class GoodsCustomerController extends BaseController {
    @Autowired
    private IGoodsCustomerService goodsCustomerService;


    /**
     * 查询客户信息管理列表
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsCustomer:query')")
    @GetMapping("/list")
    public TableDataInfo list(GoodsCustomer entity) {
        startPage();
        List<GoodsCustomer> list = goodsCustomerService.selectList(entity);
        return getDataTable(list);
    }

    /**
     * 导入客户信息管理列表
     */
    @Log(title = "导出订单产品列表", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(HttpServletRequest request, HttpServletResponse response, GoodsCustomer entity) {
        goodsCustomerService.exportData(request,response,entity);
    }

    /**
     * 获取客户信息管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsCustomer:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(goodsCustomerService.selectById(id));
    }

    /**
     * 新增客户信息管理
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsCustomer:add')")
    @Log(title = "客户信息管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody GoodsCustomer entity) {
        if (UserConstants.NOT_UNIQUE.equals(goodsCustomerService.checkNameUnique(entity.getName()))) {
            return AjaxResult.error("新增客户'" + entity.getName() + "'失败，客户已存在");
        }
        // 根据地区生成 客户编码 （地区+三位数）先查询最大编码 +1
        String codeMax = goodsCustomerService.getMaxCode();
        // 如果存在 则加1 否则返回0001
        if (codeMax != null) {
            codeMax = String.format("%04d", Integer.parseInt(codeMax) + 1);
        }

        entity.setCode(codeMax);
        return toAjax(goodsCustomerService.insert(entity));
    }

    /**
     * 修改客户信息管理
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsCustomer:edit')")
    @Log(title = "客户信息管理", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody GoodsCustomer entity) {
        return toAjax(goodsCustomerService.update(entity));
    }

    /**
     * 删除客户信息管理
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsCustomer:remove')")
    @Log(title = "客户信息管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(goodsCustomerService.deleteByIds(ids));
    }


    @Log(title = "客户信息导入数据", businessType = BusinessType.INSERT)
    @PostMapping("/importData")
    public AjaxResult importData(GoodsCustomer entity,MultipartFile file) {
        Map<String, Object> error = new HashMap<>();
        error = goodsCustomerService.importData(entity,file);
        error.put("code", 200);
        return AjaxResult.success(error);
    }
}
